package com.sayimo.maker.mall.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sayimo.framework.common.AbstractBaseService;
import com.sayimo.framework.exception.DaoException;
import com.sayimo.framework.exception.ServiceException;
import com.sayimo.maker.commons.Constant;
import com.sayimo.maker.mall.goods.dao.GoodsNormsValueStockInterfaceDao;
import com.sayimo.maker.mall.goods.service.GoodsNormsValueStockInterfaceService;
import com.sayimo.maker.mall.goods.vo.GoodsNormsStock;
import com.sayimo.maker.mall.goods.vo.GoodsNormsValueStock;
import com.sayimo.maker.mall.orders.service.OrdersGoodsInfoInterfaceService;
import com.sayimo.maker.mall.orders.vo.OrdersGoodsBuyInfo;
import com.sayimo.maker.mall.orders.vo.OrdersGoodsInfo;


@Service(value = "goodsNormsValuesInterfaceService")
public class GoodsNormsValueStockInterfaceImplService extends AbstractBaseService implements GoodsNormsValueStockInterfaceService {

	@Resource(name = "goodsNormsValueStockInterfaceDao")
	private GoodsNormsValueStockInterfaceDao goodsNormsValuesInterfaceDao;
	
	@Resource(name="ordersGoodsInfoInterfaceService")
	private OrdersGoodsInfoInterfaceService ordersGoodsInfoInterfaceService;
	
	@Autowired
	private GoodsNormsValueStockInterfaceDao goodsNormsValueStockDao;

	private static Log logger = LogFactory.getLog(GoodsNormsValueStockInterfaceImplService.class);

	@Override
	public GoodsNormsValueStock queryGoodsNormsValueStockByGoodsId(GoodsNormsValueStock goodsNormsValueStock) throws ServiceException {
		try {
			return goodsNormsValuesInterfaceDao.queryGoodsNormsValueStockByGoodsId(goodsNormsValueStock);
		} catch (DaoException de) {
			throw new ServiceException(de);
		}
	}

	@Override
	public void updateAbatementGoodsNormsStockById(Integer sctockId, Integer stockNum) throws ServiceException {
		try {
			if (stockNum > 0) {
				GoodsNormsStock goodsNormsStock = new GoodsNormsStock();
				goodsNormsStock.setId(sctockId);
				goodsNormsStock.setStockNum(stockNum);
				goodsNormsValuesInterfaceDao.updateAbatementGoodsNormsStockById(goodsNormsStock);
			}
		} catch (DaoException de) {
			throw new ServiceException(de);
		}
	}

	@Override
	public OrdersGoodsBuyInfo ordersBuyStatus(Integer[] orderIdArray, String areaCode) throws ServiceException {
		OrdersGoodsBuyInfo ogbi = new OrdersGoodsBuyInfo();
		ogbi.setStatus(true);
		ogbi.setLogistStatus(true);
		List<OrdersGoodsInfo> ordersGoodsInfos = null;
		outter: for(Integer ordersId : orderIdArray){
			ordersGoodsInfos = ordersGoodsInfoInterfaceService.queryOrdersGoodsInfoByOrdersId(ordersId);
			if(ordersGoodsInfos != null && ordersGoodsInfos.size()>0){
				inner: for(OrdersGoodsInfo ordersGoodsInfo : ordersGoodsInfos){
					Map<String, Object> pm = new HashMap<String, Object>();
					try {
						pm.put("ordersId", ordersId);
						pm.put("goodsId", ordersGoodsInfo.getGoodsId());
						pm.put("goodsNormsValueId", ordersGoodsInfo.getNormsValueId());
						
						//获取活动商品库存
						Map<String, Object> activityGoodsInfo = goodsNormsValuesInterfaceDao.getActivityStockNum(pm);
						int activityStockNum=0;
						if(activityGoodsInfo!=null&&activityGoodsInfo.get("identifier")!=null&&!activityGoodsInfo.get("identifier").equals("")){
							activityStockNum=(int)activityGoodsInfo.get("activityNum");
							if (activityStockNum < (int)ordersGoodsInfo.getBuyNum() || activityStockNum == 0) {
								logger.info("库存不足");
								ogbi.setStatus(false);
								ogbi.setGoodsId(ordersGoodsInfo.getGoodsId());
								ogbi.setGoodsNormsId(ordersGoodsInfo.getNormsValueId());
								return ogbi;
							}
						}
						// 判断库存
						Integer stockNum = goodsNormsValuesInterfaceDao.queryGoodsNormsValueStockByGoodsIdNormsValueId(pm);
						stockNum = (int)stockNum;
						if (stockNum < (int)ordersGoodsInfo.getBuyNum() || stockNum == 0 || (int)ordersGoodsInfo.getBuyNum() == 0) {
							logger.info("库存不足");
							ogbi.setStatus(false);
							ogbi.setGoodsId(ordersGoodsInfo.getGoodsId());
							ogbi.setGoodsNormsId(ordersGoodsInfo.getNormsValueId());
							return ogbi;
						}
						
						// 判断是否支持发货到指定地址
						if(Integer.parseInt(areaCode)>0){
							pm.put("areaCode", areaCode);
							Map logistMap = ordersGoodsInfoInterfaceService.queryGoodsLogist(pm);
							if (MapUtils.isNotEmpty(logistMap)) { 
								if (logistMap.get("logisticsCostId") != null && logistMap.get("areaCodeList") == null) {
									ogbi.setLogistStatus(false);
									return ogbi;
								}
							}
						}
					} catch (DaoException e) {
						throw new ServiceException(e);
					}
				}
			}
		}
		return ogbi;
	}

	/**
	 * 
	 * @MethodName searchGoodsByName
	 * @Description 商品详情切换规格
	 * 
	 * @author chenpy
	 * @date 2016年1月6日 下午3:25:50
	 * @param
	 * @return Map
	 * @VERSION 1.0.0
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Map changeGoodsNormsValue(Integer goodsId, Integer normsValueId) throws ServiceException {
		Map resultMap = new HashMap();
		

		try {
			Map<String, Object> map = new HashMap();
			map.put("goodsId", goodsId);
			map.put("normsValueId", normsValueId);
			map = goodsNormsValuesInterfaceDao.changeGoodsNormsValue(map);
			resultMap.put(Constant.STATUS, Constant.STATUS_SUCCESS);
			resultMap.put(Constant.DATA, map);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		return resultMap;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public int batchUpdateGoodsStock(List list) throws ServiceException {
		try {
			return this.goodsNormsValuesInterfaceDao.batchUpdateGoodsStock(list);
		} catch (DaoException e) {
			throw new ServiceException(e);
		}
	}

	public Map<String,Object> getFirstGoodsNormsValueStockByGoodsId(Integer goodsId) throws DaoException {
		return this.goodsNormsValuesInterfaceDao.getFirstGoodsNormsValueStockByGoodsId(goodsId);
	}
	
	@Override
	public Map<String, Object> querySkuCodeByNomalId(Map<String, Object> paramMap) throws DaoException {
		return goodsNormsValuesInterfaceDao.querySkuCodeByNomalId(paramMap);
	}
	
	@Override
	public Integer updateStockNum(Map<String, Object> paramMap) throws ServiceException {
		try {
			return goodsNormsValuesInterfaceDao.updateStockNum(paramMap);
		} catch (DaoException e) {
			throw new ServiceException();
		}
	}

	/** 
	  * @package com.sayimo.school.mall.service.goods
	  * @author wangyang
	  * @Title:  getNormsValueStockByMap
	  * @Description: 按ID获取GoodsNormsValueStock
	  * @param 
	  * @throws 
	  * @return GoodsNormsValueStock
	  * @date 2016年7月15日
	  */
	public Map<String, Object> getNormsValueStockByMap(Map<String, Object> paramMap) throws ServiceException {
		try {
			return goodsNormsValueStockDao.getNormsValueStockByMap(paramMap);
		} catch (DaoException e) {
			logger.error(e);
			throw new ServiceException(e);
		}
	}
}
